<?php
/**
 * アプリケーションブートストラップ
 *
 * @package		Bootstrap
 * @author		tanaka
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	/**
	 * フロントコントローラー初期化
	 *
	 * @return Zend_Controller_Front
	 */
	protected function _initFrontController()
	{
		$front = Zend_Controller_Front::getInstance();
		$front->setControllerDirectory(CONTROLLER_PATH)
			->getRouter()
			->addConfig(App::getInstance()->route);
			
		Zend_Controller_Action_HelperBroker::addPath(
			MODELS_PATH . '/Controller/Action/Helper', 'Controller_Action_Helper'
		);
		
		if(_DEBUG){
			//// 設定類のデバッガ
			//$front->registerPlugin(new Controller_Plugin_Debug_Application());
			
			// ディスパッチャのデバッガ
			$front->registerPlugin(new Controller_Plugin_Debug_Dispatched());
			
			// リクエストのデバッガ
			$front->registerPlugin(new Controller_Plugin_Debug_Request($front->getRequest()));
			
			$front->registerPlugin(new Controller_Plugin_Debug_Js_Input());
		}
		
		return $front;
	}

	/**
	 * Viewの初期化
	 *
	 * @return	View_Smarty_Layout
	 */
	protected function _initView()
	{
		//Smartyセットアップ
		$config = App::getInstance()->view;
		require_once('Smarty/libs/Smarty.class.php');
		$view = new View_Smarty_Layout(null, $config->smarty->toArray());

		//Zend_Layoutの開始
		Zend_Layout::startMvc($config->layout);
		$layout = Zend_Layout::getMvcInstance();
		$layout->setView($view);
		
		//ヘルパーに追記
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view)
			->setViewBasePathSpec($view->getEngine()->templates_dir)
			->setViewScriptPathSpec(':controller/:action.:suffix')
			->setViewScriptPathNoControllerSpec(':action.:suffix')
			->setViewSuffix($layout->getViewSuffix());
		
		if(_DEBUG){
			Zend_Controller_Front::getInstance()->registerPlugin(new Controller_Plugin_Debug_SmartyAssigned($view));
		}

		return $view;
	}

	/**
	 * DBの初期化
	 *
	 * @return	Zend_Db_Adapter_Abstract
	 */
	protected function _initDb()
	{
		$dbAdapter = Db_Manager::getInstance()->getAdapter();
		Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
		
		if(_DEBUG){
			// SQLデバッガ
			Zend_Controller_Front::getInstance()->registerPlugin(new Controller_Plugin_Debug_Sql($dbAdapter));
		}
			
		return $dbAdapter;
	}
}
